package top.lyc829.zzl_android.ui.profile.user_fans;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.io.IOException;
import java.util.List;

import okhttp3.Response;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.bean.UserInfoBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

public class UserFansViewModel extends ViewModel {
    private MutableLiveData<List<UserInfoBean>> userFansLiveData;
    private MutableLiveData<Integer> isFollowLiveData;

    private MutableLiveData<UserInfoBean> userInfoBeanLiveData;

    public MutableLiveData<List<UserInfoBean>> getUserFansLiveData() {
        if(userFansLiveData==null)
        {
            userFansLiveData=new MutableLiveData<>();
        }
        return userFansLiveData;
    }

    public MutableLiveData<Integer> getIsFollowLiveData() {
        if (isFollowLiveData == null) {
            isFollowLiveData = new MutableLiveData<>();
        }
        return isFollowLiveData;
    }

    public MutableLiveData<UserInfoBean> getUserInfoBeanLiveData()
    {
        if(userInfoBeanLiveData==null)
        {
            userInfoBeanLiveData=new MutableLiveData<>();
        }
        return userInfoBeanLiveData;
    }

    public void getUserFansList(int userId) {
        Network.getUserFansList(userId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                ResultBean<List<UserInfoBean>> resultBean = JsonUtilNew.fromJsonArray(resultStr, UserInfoBean.class);
                if (resultBean.getCode().equals(200)) {
                    getUserFansLiveData().postValue(resultBean.getData());
                } else
                    failed(new IOException(resultBean.getMsg()));
            }

            @Override
            public void failed(IOException e) {
                getUserFansLiveData().postValue(null);
                Utils.Sout(e.getMessage());
            }
        });
    }
}
